package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断商品是否在购物车中
         ShoppingCart shoppingCart = new ShoppingCart();
         BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

         shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList= shoppingCartMapper.list(shoppingCart);
        //如果在购物车中，则更新数量，将数量+1
         if(shoppingCartList.size()>0&&shoppingCartList!=null){
             shoppingCart=shoppingCartList.get(0);//查询出来结果长度一定是1
             shoppingCart.setNumber(shoppingCart.getNumber()+1);
             shoppingCartMapper.updateNumberById(shoppingCart);
             //如果不在购物车中，则将数据插入
         }else {
             //判断当前插入的数据是菜品还是套餐
             Long dishId = shoppingCart.getDishId();
             if(dishId!=null){
              Dish dish=dishMapper.getbyId(dishId);
              shoppingCart.setName(dish.getName());
              shoppingCart.setImage(dish.getImage());
              shoppingCart.setAmount(dish.getPrice());
             }else {
                 Setmeal setmeal=setmealMapper.getById(shoppingCartDTO.getSetmealId());
                 shoppingCart.setName(setmeal.getName());
                 shoppingCart.setImage(setmeal.getImage());
                 shoppingCart.setAmount(setmeal.getPrice());

             }
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartMapper.insert(shoppingCart);
         }


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {

        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list.size()>0&&list!=null){
            ShoppingCart shoppingCart1=list.get(0);
            if(shoppingCart1.getNumber()>1){
                //商品数量大于1则将数量减去1
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                shoppingCartMapper.updateNumberById(shoppingCart1);
            }else{
                //商品数量等于1，则直接删除商品
                shoppingCartMapper.deleteById(shoppingCart1);

            }
        }

    }
}
